home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / sspell14.zip / CHECK.C < prev    next >
C/C++ Source or Header  |  1992-07-06  |  6KB  |  201 lines

  1. /* **************************************************************** */
  2. /*             sspell - similar to Unix spell                       */
  3. /*                        version 1.4                               */
  4. /*                                                                  */
  5. /* Author: Maurice Castro                                           */
  6. /* Release Date:  4 Jul 1992                                         */
  7. /* Bug Reports: maurice@bruce.cs.monash.edu.au                      */
  8. /*                                                                  */
  9. /* This code has been placed by the Author into the Public Domain.  */
  10. /* The code is NOT covered by any warranty, the user of the code is */
  11. /* solely responsible for determining the fitness of the program    */
  12. /* for their purpose. No liability is accepted by the author for    */
  13. /* the direct or indirect losses incurred through the use of this   */
  14. /* program.                                                         */
  15. /*                                                                  */
  16. /* Segments of this code may be used for any purpose that the user  */
  17. /* deems appropriate. It would be polite to acknowledge the source  */
  18. /* of the code. If you modify the code and redistribute it please   */
  19. /* include a message indicating your changes and how users may      */
  20. /* contact you for support.                                         */
  21. /*                                                                  */
  22. /* The author reserves the right to issue the official version of   */
  23. /* this program. If you have useful suggestions or changes for the  */
  24. /* code, please forward them to the author so that they might be    */
  25. /* incorporated into the official version                           */
  26. /*                                                                  */
  27. /* Please forward bug reports to the author via Internet.           */
  28. /*                                                                  */
  29. /* **************************************************************** */
  30.  
  31. #include "check.h"
  32. #include "cache.h"
  33. #include "file.h"
  34. #include <stdio.h>
  35. #include "config.h"
  36. #include "strfn.h"
  37. #include "root.h"
  38. #include "utility.h"
  39. #include "stop.h"
  40.  
  41. extern FILE *fout;
  42. extern int prtderive;
  43. extern int prtroot;
  44.  
  45. int isupper(a)
  46. char a;
  47. {
  48.    if (('A' <= a) && (a <= 'Z')) return(1);
  49.    return(0);
  50.    }
  51.  
  52. int isnumber(a)
  53. char *a;
  54. {
  55.     while (*a != NULL)
  56.     {
  57.         if (!(('0' <= *a) && (*a <= '9'))) return (0);
  58.         a++;
  59.         }
  60.     return(1);
  61.     }
  62.  
  63. int chkcaps(dict, test)
  64. char *dict;
  65. char *test;
  66. {
  67.    /* check that any capitalized letter in the dictionary is matched */
  68.    /* by a capitalized letter in the word */
  69.    while (*dict != NULL)
  70.    {
  71.       if ((isupper(*dict)) && (*dict != *test)) 
  72.          return(0);
  73.       dict++;
  74.       test++;
  75.       }
  76.    return(1);
  77.    }
  78.  
  79. int checkword(word)
  80. char *word;
  81. {
  82.    char indict[MAXSTR];
  83.  
  84.    if (word == NULL)
  85.       return(0); /* fail */
  86.    if (searchcache(word,indict))
  87.    {
  88.        /* if it is not an exact match - check a bit more closely */
  89.        if (strcmp(indict,word))
  90.           if (!chkcaps(indict,word)) 
  91.              return(0);
  92.        }
  93.    else
  94.    {
  95.        if (searchfile(word,indict))
  96.        {
  97.            /* something close is in the file put it in the cache */
  98.            addtocache(indict);
  99.            if (strcmp(indict,word))
  100.               if (!chkcaps(indict,word)) 
  101.                  return(0);
  102.            }
  103.        else
  104.            return(0);
  105.        }
  106.    return(1);
  107.    }
  108.  
  109. void dump(a)
  110. WORDLST *a;
  111. {
  112.    while (a != NULL)
  113.    {
  114.       fprintf(fout,"=%s\n",a->word); 
  115.       a = a->next;
  116.       }
  117.    }
  118.  
  119. int checkgroup(word)
  120. char *word;
  121. {
  122.    WORDLST *a;
  123.    WORDLST *b;
  124.  
  125.    /* earliest opportunity to check if it is a number after looking for */
  126.    /* hyphens */
  127.  
  128.    if (isnumber(word)) return(1);
  129.    if (checkword(word)) return(1);  /* try the dictionary first */
  130.    if (instop(word))             /* we know this is not a word. Give up */
  131.     return(0);
  132.  
  133.    /* the word is not in the dictionary try to find root */
  134.  
  135.    root(word, &a); 
  136.    b = a;
  137.    while (a != NULL)
  138.    {
  139.       if (checkword(a->word)) 
  140.       {
  141.          if (prtderive)
  142.          {
  143.             if ((a->prefix != NULL) && (a->suffix != NULL)) fprintf(fout,"%s%s%s\n",
  144.                 a->prefix, a->word, a->suffix);
  145.             else if (a->prefix != NULL) fprintf(fout,"%s%s\n",
  146.                 a->prefix, a->word);
  147.             else if (a->suffix != NULL) fprintf(fout,"%s%s\n",
  148.                 a->word, a->suffix);
  149.             }
  150.          if (prtroot) 
  151.             dump(b);
  152.          destroy(b);
  153.          return(1);  /* found it! */
  154.          }
  155.       a = a->next;
  156.       }
  157.    destroy(b);
  158.    return(0);
  159.    }
  160.  
  161. int checkhyphen(a)
  162. char *a;
  163. {
  164.    char *hold;
  165.    char *token;
  166.    char cpstr[MAXSTR];
  167.  
  168.    if (checkgroup(a)) return(1);
  169.    strcpy(cpstr, a);
  170.    token = strltok(cpstr,"-",&hold);
  171.    while (token != NULL)
  172.    {
  173.        if (!checkgroup(token)) return(0);
  174.        token = strltok(NULL,"-",&hold);
  175.        } 
  176.    return(1);
  177.    }
  178.  
  179. void checkspell(f)
  180. FILE *f;
  181. {
  182.    char instr[MAXSTR];
  183.    char *token;
  184.    char *hold;
  185.  
  186.    while (!feof(f))
  187.    {
  188.        if (fgets(instr,MAXSTR-1,f)==NULL)
  189.           break;
  190.        token = strltok(instr,SEPSTR,&hold); 
  191.        while (token != NULL)
  192.        {
  193.            if (!checkhyphen(token))
  194.            {
  195.                fprintf(fout,"%s\n",token);
  196.                }
  197.            token = strltok(NULL,SEPSTR,&hold); 
  198.            }
  199.        }
  200.    }
  201.